package com.znsd.zl.dict.controller;

import com.znsd.zl.dict.entity.DictItemEntity;
import com.znsd.zl.dict.service.IDictItemService;
import com.znsd.zl.utils.BaseController;
import com.znsd.zl.utils.PageUtils;
import com.znsd.zl.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;


/**
 * @ClassName DictController
 * @Description TODO
 * @Author zhubin
 * @Date 2020/8/17 19:52
 * @Version v1.0
 */
@Slf4j
@RestController
@RequestMapping("zl/dictItem")
public class DictItemController extends BaseController<IDictItemService, DictItemEntity,Integer> {

	@Override
	@PostMapping
	public R save(@RequestBody DictItemEntity t) {
		try {
			log.info("添加信息data===>{}", t);
			return service.save(t) ? R.ok() : R.error("添加失败");
		}catch (Exception e){
			e.printStackTrace();
			return R.error();
		}
	}

	@Override
	@DeleteMapping
	public R deleteBatch(@RequestBody List<Integer> ids) {
		try {
			log.info("批量删除的ids===>{}", ids);
			service.removeByIds((Collection<? extends Serializable>) ids);
		}catch (Exception e){
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}
		return R.ok();
	}

	@Override
	@PutMapping
	public R update(@RequestBody DictItemEntity t) {
		try {
			log.info("修改的新信息data===>{}", t);
			return service.updateById(t) ? R.ok() : R.error("修改失败");
		}catch (Exception e){
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}
	}

	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = this.service.queryPage(params);
		return R.ok().put("page",page);
	}

	@GetMapping("/findAllText")
	public R queryTextByDictType(String dictType) {
		List<DictItemEntity> dictItems = this.service.queryTextByDictType(dictType);
		return R.ok().put("data",dictItems);
	}

	@GetMapping("/findOneText")
	public R queryTextByDictTypeAndValue(String dictType, String itemValue) {
		DictItemEntity dictItem = this.service.queryTextByDictTypeAndValue(dictType, itemValue);
		return R.ok().put("data",dictItem);
	}
}
